<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ResourceopController.php 465 2012-02-12 05:33:23Z beimuaihui $
 */



class System_ResourceopController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'ResourceOp' );
		$this->resourceOp = new ResourceOp ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;

		BAOGG_LANGUAGE::loadFile("resource");

		$params=$this->getRequest()->getParams();
		$form_url=$params;
		$resource_id=$params['resource_id'];

		Zend_Loader::loadClass("Baogg_View_Extgrid");

		$cm=array();
		$cm['resource_op_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['resource_op_name']=array("sortable"=>true);
		$cm['resource_op_code']=array("sortable"=>true);
		$cm['resource_op_pid']=array("hidden"=>true);
		$cm['state']=array("sortable"=>true);
		$cm['ordering']=array("sortable"=>true);
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;

		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete'));

		$this->view->form_url=BAOGG_BASE_URL."system/resourceop/save/resource_id/{$resource_id}";
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		$this->view->resource_id=$resource_id;

		Zend_Loader::loadClass ( 'Resource' );
		$resource=new Resource();
		$rs_resource=$resource->getOne($resource_id);
		$resource_name=(is_array($rs_resource) && count($rs_resource)>0)?$rs_resource[0]['resource_name']:"";

		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>300,"width"=>600));
		$form->addItem(0,array('name'=>'resource_name','xtype'=>'displayfield','value'=>$resource_name));
		$form->addItem(1,array('name'=>'resource_op_name'));
		$form->addItem(1,array('name'=>'resource_op_code'));
		
		$store_pid=$this->resourceOp->getComboTree(array('resource_op_pid'=>0,'resource_id'=>$resource_id),array('sort'=>'ordering','dir'=>'asc'),null,array('resource_op_id','resource_op_name','resource_op_pid'));
		array_unshift($store_pid,array('0',BAOGG_LANGUAGE::get('please_select')));
		$form->addItem(1,array('name'=>'resource_op_pid','xtype'=>'combo','store'=>$store_pid));
		$form->addItem(2,array('name'=>'state','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(2,array('name'=>'ordering'));
		$form->addItem(2,array('name'=>'resource_id',"xtype"=>"hidden","hideLabel"=>true,'value'=>$resource_id));
		$form->addItem(2,array('name'=>'resource_op_id',"xtype"=>"hidden","hideLabel"=>true));
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));
		$this->view->form=$form;


		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction() {
		$post=$this->_request->getPost ();
		$resource_id=$this->_request->getParam('resource_id');
		$where=array();$order=array();$limit=array();
		foreach((array)$post as $k=>$v){
			if($k=="start" || $k=="limit"){
				$limit[$k]=$v;
			}else if($k=="sort" || $k=="dir"){
				$order[$k]=$v;
			}else{
				if($k=="field_id"  ){
					if($v && is_numeric($v)){
						$v=(int)$v;
						$where[$k]=$v;
					}
				}else{
					$v='%'.$v.'%';
					$where[$k]=$v;
				}

			}
		}
		if(!$order){
			$order['sort']='ordering';
			$order['dir']='asc';
		}
		$where['resource_id']=(int)$resource_id;
		$data=$this->resourceOp->getListTree($where,$order,$limit,array('resource_op_id','resource_op_name','resource_op_pid','*'));
		$cnt=$this->resourceOp->getList($where,null,null,"count(*) as cnt");
		//echo Baogg_Db::debugDb(Zend_Registry::get('db'));exit;
		$this->view->grid = Zend_Json::encode(array("count"=>$cnt[0]['cnt'],"data"=>$data));
	}


	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}

	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}

	/**
	 * The delete action
	 */
	function deleteAction() {

		$ret=array("success"=>false,"msg"=>"delete_fail");
		$id = Baogg_Controller_Action::_getParam ( 'ids' );
			
		 

		if ($this->resourceOp->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}

		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();

	}

	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;

		$ret=array("success"=>false,"msg"=>"save_fail");

		// echo '<pre>';print_r($this->resourceOp->info(ResourceOp::METADATA));exit;
		//check form
		if (!$this->_request->isPost ())
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}

		$formData = $this->_request->getPost ();

		if(!$formData)
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}

		$formData['resource_id']= $this->_request->getParam("resource_id");
		$arr=$this->resourceOp->filterForm($formData);
		$action=$this->getRequest()->getParam('action');
		$rs = $this->resourceOp->getSame($arr);
		if($action=='edit'){
			//update record
			//$permission_type = 2;
				
			if(!$rs  ||  count($rs)==1 &&  $rs[0] == $arr["resource_op_id"] ){
				$this->resourceOp->_update($arr,$arr["resource_op_id"]);
				$ret['success'] = true;
				$ret['msg'] = "save_ok";
			}else{
				$ret['msg'] = "exist_same_record";
			}
				
		}else{
			//insert record
			//$permission_type = 1;
			if($rs){
				$ret['msg'] = "exist_same_record";
			}else{
				unset($arr["field_id"]);
				$ret['success'] = $this->resourceOp->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}



		//$db=Zend_Registry::get("db");echo Baogg_Db::debugDb($db);exit;
		echo Baogg_Language::outputResult($ret);

		$this->_helper->viewRenderer->setNoRender ();


	}

}
